<template>
  <div>
    <div style="width:500px; margin:20px auto;">
      <el-input placeholder="请输入企业名称" v-model="input3" class="input-with-select" clearable>
        <el-button slot="append" icon="el-icon-search" @click="search">搜索</el-button>
      </el-input>
    </div>
    <el-table :data="form" border>
      <el-table-column prop="comname" label="企业名称" width="170"></el-table-column>
      <el-table-column prop="iphone" label="联系电话" width="170"></el-table-column>
      <el-table-column prop="address" label="企业地址" width="170"></el-table-column>
      <el-table-column prop="introduce" label="公司简介"></el-table-column>
      <el-table-column prop="manage" label="从事行业" width="100"></el-table-column>
      <el-table-column prop="state" label="状态" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.state"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="changestate(scope.row.id,scope.row.state)"
          />
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template  slot-scope="scope">
          <el-button @click="showEditDialog(scope.row.id)" type="text" size="small"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!--    编辑-->
    <el-dialog
      title="编辑"
      :data="edForm"
      ref="edFormRef"
      :visible.sync="editDialog"
    >
      <el-form status-icon label-width="70px">
        <el-form-item prop="iphone" label="手机号">
          <el-input
            type="text"
            v-model="edForm.iphone"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            show-password
            type="password"
            v-model="edForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item label="企业名称" prop="name">
          <el-input type="text" v-model="edForm.comname"></el-input>
        </el-form-item>
        <el-form-item label="从事行业" prop="manage">
          <el-input type="text" v-model="edForm.manage"></el-input>
        </el-form-item>
        <el-form-item label="企业地址" prop="address">
          <el-input type="text" v-model="edForm.address"></el-input>
        </el-form-item>
        <el-form-item label="公司简介" prop="introduce">
          <el-input type="text" v-model="edForm.introduce"></el-input>
        </el-form-item>
      </el-form>
      <div class="register-button" slot="footer">
        <el-button @click="editDialog = false">取 消</el-button>
        <el-button type="primary" @click="editInfo('edFormRef')"
          >提交修改</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      input3:null,
      form: [
      ],
      edForm: {},
      editDialog: false,
    };
  },
  methods: {
    //搜索公司
    search(){
      var url = this.$store.state.ip + "/com/selectCom1";
      // 参数
      let param = new URLSearchParams();
      param.append("comname",this.input3);
      this.$axios({
        method: "post",
        url: url,
        data: param,
      }).then((res) => {
        console.log(res.data);
        this.form = res.data;
      });
    },
    // 查询所有的公司
    selectCom(){
      var url = this.$store.state.ip + "/com/selectCom";
      // 参数
      let param = new URLSearchParams();
      this.$axios({
        method: "post",
        url: url,
        data: param,
      }).then((res) => {
        console.log(res.data);
        this.form = res.data;
      });
    },
    // 显示编辑框
    showEditDialog(id) {
      this.editDialog = true;
      this.selectComById(id);
    },
    // 提交修改
    editInfo() {
      var id = this.$store.state.userId;
      console.log("获取userId:", id);
      var url = this.$store.state.ip + "/com/updateCom";
      // 参数
      let param = new URLSearchParams();
      param.append("id", id);
      param.append("iphone", this.edForm.iphone);
      param.append("comname", this.edForm.comname);
      param.append("password", this.edForm.password);
      param.append("address", this.edForm.address);
      param.append("manage", this.edForm.manage);
      param.append("introduce", this.edForm.introduce);
      this.$axios({
        method: "post",
        url: url,
        data: param,
      }).then((res) => {
        console.log(res.data);
        this.$router.go(0)
      });
      this.editDialog = false;
    },
    // 改变企业状态
    changestate: function (id,state) {
      var url = this.$store.state.ip + "/com/updateState";
      // 参数
      let param = new URLSearchParams();
      param.append("id", id);
      param.append("state", state);
      this.$axios({
        method: "post",
        url: url,
        data: param,
      }).then((res) => {
        console.log(res.data);
      });
    },
    selectComById(id) {
      var url = this.$store.state.ip + "/com/selectComById";
      // 参数
      let param = new URLSearchParams();
      param.append("id", id);
      this.$axios({
        method: "post",
        url: url,
        data: param,
      }).then((res) => {
        console.log(res.data);
        this.edForm = res.data[0];
      });
    },
  },
  beforeMount() {
    this.selectCom();
  },
};
</script>